﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace SDService.Common
{

    /// <summary>
    /// 系统字典，各种常量，错误码存在于此
    /// </summary>
    public class SystemConstant
    {
        public static string GetCode(string value)
        {
            if (SystemCodes == null)
                return "39999";

            SystemCode code = SystemCodes.FirstOrDefault(o => o.Name.Equals(value));
            return code == null ? "39999" : code.Code.ToString();
        }

        /// <summary>
        /// 获取CODE对应的MSG
        /// </summary>
        /// <param name="keyCode"></param>
        /// <returns></returns>
        public static string GetValue(string keyCode)
        {
            if (SystemCodes == null)
                return "代码未定义";

            SystemCode code = SystemCodes.FirstOrDefault(o => o.Code.Equals(keyCode));
            return code == null ? "代码未定义" : code.Name;

        }

        public static List<SystemCode> GetInfo(string code)
        {
            if (string.IsNullOrEmpty(code))
                return SystemCodes;
            SystemCode info = SystemCodes.FirstOrDefault(o => o.Code == code);
            if (info == null)
                return null;
            List<SystemCode> infos = new List<SystemCode>
            {
                info
            };
            return infos;
        }

        public static List<SystemCode> SystemCodes { get; private set; }

        static SystemConstant()
        {
            SystemCodes = new List<SystemCode>();

            #region 错误码
            //systemCodes = new Dictionary<string, SystemCode>();
            SystemCodes.Add(new SystemCode("3", "错误码", "0"));
            SystemCodes.Add(new SystemCode("30000", "成功", "3"));

            //--平台错误
            SystemCodes.Add(new SystemCode("30100", "错误", "3"));
            SystemCodes.Add(new SystemCode("30110", "数据库错误", "30100"));
            SystemCodes.Add(new SystemCode("30111", "数据不存在", "30110"));
            SystemCodes.Add(new SystemCode("30112", "数据查询失败", "30110"));
            SystemCodes.Add(new SystemCode("30113", "数据更新失败", "30110"));

            SystemCodes.Add(new SystemCode("30120", "逻辑错误", "30100"));
            SystemCodes.Add(new SystemCode("30121", "传入的参数错误", "30120"));
            SystemCodes.Add(new SystemCode("30122", "token错误", "30120"));
            SystemCodes.Add(new SystemCode("30123", "传入的数据错误", "30120"));

            SystemCodes.Add(new SystemCode("39998", "未定义的错误", "3"));
            SystemCodes.Add(new SystemCode("39999", "未知错误", "3"));

            #endregion


        }
    }

    /// <summary>
    /// 系统用码
    /// </summary>
    public class SystemCode
    {
        public string Code { get; set; }
        public string Name { get; set; }
        public string Remarks { get; set; }

        public string Parent { get; set; }

        public SystemCode()
        {

        }

        public SystemCode(string code, string name)
        {
            Code = code;
            Name = name;
        }

        public SystemCode(string code, string name, string parent)
        {
            Code = code;
            Name = name;
            Parent = parent;
        }
    }
}




